package com.melody.android.ui.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.view.GravityCompat
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import com.melody.android.databinding.FragmentDiscoveryBinding
import com.melody.android.logic.model.RecommendSongList
import com.melody.android.logic.model.Song
import com.melody.android.logic.util.setPrimaryDesign
import com.melody.android.logic.util.setRecommendSongLists
import com.melody.android.logic.util.setStarSongList
import com.melody.android.logic.util.showToast
import com.melody.android.ui.viewModel.SongViewModel

class DiscoveryFragment : Fragment() {

    private lateinit var binding: FragmentDiscoveryBinding
    private lateinit var songViewModel: SongViewModel

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
    ): View {
        binding = FragmentDiscoveryBinding.inflate(inflater, container, false)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        // 获取MainActivity所持有的SongViewModel
        songViewModel = ViewModelProvider(requireActivity())[SongViewModel::class.java]
        initView()
    }

    private fun initView() {
        // 设置推荐歌单recyclerView
        binding.rvRecommendSongLists.setRecommendSongLists(songViewModel.recommendSongLists.value as ArrayList<RecommendSongList>)
        // 设置精选单曲recyclerView
        binding.rvStarSongList.setStarSongList(songViewModel, songViewModel.starSongList.value as ArrayList<Song>)
        binding.imgUserImage.setPrimaryDesign()
        binding.menuIcon.setOnClickListener {
            binding.drawerLayout.openDrawer(GravityCompat.START)
        }
        binding.llUserInfo.setOnClickListener {
            "个人信息".showToast()
        }
        binding.llUserFriend.setOnClickListener {
            "我的好友".showToast()
        }
        binding.llCommunity.setOnClickListener {
            "社区".showToast()
        }
        binding.llLogout.setOnClickListener {
            "注销".showToast()
        }
    }

}